#include"Sock.hpp"
#include<unistd.h>
void Usage(std::string proc)
{
  cout <<"Usage\n\t "<< proc <<" server_ip" <<" server_port" << endl;
  exit(1);
}
int main(int argc,char* argv[])
{
  if(argc != 3)
  {
    Usage(argv[0]);
  }
  int sock = Sock::Socket();
  std::string ip = argv[1];
  uint16_t port = atoi(argv[2]);
  Sock::Connect(sock,ip,port);
  
  while(1)
  {
      cout <<"Please Enter:";
    //客户端:获取用户输入，然后发送给服务端,然后获取服务端的响应
    char buf[1024] = {0};
    fgets(buf,sizeof(buf),stdin);//注意:从键盘当中读取，会把\n也读取到
    buf[strlen(buf) - 1] = 0;//hello\n\0  strlen(buf)=6 把\n位置置为0
    write(sock,buf,sizeof(buf)-1); //写入的时候不写入\0,因为\0只是C语言层面的结束标识符，而写入关心的是内容

    read(sock,buf,sizeof(buf));
    cout <<"server #" << buf << endl;
  }
  return 0;
}
